home *** CD-ROM | disk | FTP | other *** search
/ Power Utilities / Power Utilities.iso / utility / pro36 / chd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-01-28  |  2.7 KB  |  100 lines

  1. #include <dos.h>
  2. #include <stdio.h>
  3. #define TRUE  1
  4. #define FALSE  0
  5. char    curdir[64], newdir[64];
  6. int    newdirlen, DONE;
  7. main(argc,argv)
  8. int    argc;
  9. char    *argv[];
  10. {
  11.     char    *fname, rootdir[64];
  12.     int    i;
  13.  
  14.     if (argc == 1) {                    /* If no arguments specified, */
  15.         getcwd(curdir,64);            /*  then print the current        */
  16.         printf("%s\n",curdir);        /*  directory name and return */
  17.         return;
  18.     }
  19.  
  20.     if (argc > 2) {                    /* Invalid number of arguments */
  21.         printf("Usage - CHD [pathname]\n");
  22.         return;
  23.     }
  24.  
  25.     strcpy(newdir,strupr(argv[1]));    /* Make global copy of path name, */
  26.                                                 /*  converting to uppercase.      */
  27.  
  28.     if (chdir(newdir)) {                    /* Try make NEWDIR the new dir.    */
  29.         newdirlen = strlen(newdir);    /* If fail, get name length.        */
  30.         if (newdir[0] != '\\') {        /* Make a copy of the path name    */
  31.             rootdir[0] = '\\';            /*  that has a backslash as the     */
  32.             strcpy(rootdir+1,newdir);    /*  first character.                    */
  33.         }
  34.         else strcpy(rootdir,newdir);
  35.  
  36.         if (chdir(rootdir)) {            /* Try this name.                        */
  37.             DONE = FALSE;                    /* If this fails, search down       */
  38.             searchdirs("\\");                /*  the directory tree.                  */
  39.             if (!DONE) printf("Invalid directory\n");
  40.         }
  41.     }
  42. }
  43.  
  44.  
  45. searchdirs(fname)
  46. char    *fname;
  47. {
  48.     struct FIND *pf;
  49.     int    i;
  50.     char    newname[64];
  51.     char    pfsave[sizeof(*pf)];
  52.  
  53.     i = index(fname,newdir[0]);
  54.     while (i != NULL && strncmp(newdir,i,newdirlen) != 0) i = index(++i,newdir[0]);
  55.     if (i != NULL) {
  56.         DONE = TRUE;
  57.         if (fname[strlen(fname)-1] == '\\') fname[strlen(fname)-1] = '\0';
  58.         chdir(fname);
  59.         return;
  60.     }
  61.  
  62.     strcpy(newname,fname);        /*  the rightmost slash.            */
  63.     strcat(newname,"*.");        /*                                            */
  64.  
  65.     pf = findfirst(newname,0x10);    /* Find the first subdir        */
  66.  
  67.     /* Keep searching for more sudirs if the one we found wasn't a subdir,    */
  68.     /*  or in case it was . or ..                                                            */
  69.     while (pf != 0 && ((*pf).name[0] == '.' || (*pf).attribute != 0x10)) pf = findnext();
  70.  
  71.     for (i=0; i < sizeof(*pf); i++) pfsave[i] = (*pf).reserved[i];
  72.  
  73.     if (pf != 0) {
  74.         strcpy(newname,fname);            /* Build name of subdir to    */
  75.         strcat(newname,(*pf).name);    /*  find.                          */
  76.         strcat(newname,"\\");            /*                                    */
  77.  
  78.         searchdirs(newname);                /* Apply routine recursively */
  79.         if (DONE) return;
  80.         while( pf != 0) {
  81.             for (i=0; i < sizeof(*pf); i++) (*pf).reserved[i] = pfsave[i];
  82.  
  83.             if (pf != 0)
  84.             do {
  85.                 pf = findnext();        
  86.             } while (pf != 0 && ((*pf).name[0] == '.' || (*pf).attribute != 0x10));
  87.  
  88.             if (pf != 0) {
  89.                 for (i=0; i < sizeof(*pf); i++) pfsave[i] = (*pf).reserved[i];
  90.  
  91.                 strcpy(newname,fname);
  92.                 strcat(newname,(*pf).name);
  93.                 strcat(newname,"\\");
  94.                 searchdirs(newname);
  95.                 if (DONE) return;
  96.             }
  97.         }
  98.     }
  99. }
  100.